package discovery

import (
	"errors"
	"gitee.com/vrv_media/go-micro-framework/registry"
	"gitee.com/vrv_media/go-micro-framework/registry/discovery/consul"
	"gitee.com/vrv_media/go-micro-framework/registry/discovery/etcd"
	"gitee.com/vrv_media/go-micro-framework/registry/discovery/eureka"
	"gitee.com/vrv_media/go-micro-framework/registry/discovery/kubernetes"
	"gitee.com/vrv_media/go-micro-framework/registry/discovery/nacos"
	"gitee.com/vrv_media/go-micro-framework/registry/discovery/native_config"
	"gitee.com/vrv_media/go-micro-framework/registry/discovery/zookeeper"
	"gitee.com/vrv_media/go-micro-framework/registry/options"
	"strings"
)

func GetDiscoveryWithDiscoveryOpe(discovery *options.DiscoveryOptions) (registry.Discovery, error) {
	if discovery == nil {
		return nil, errors.New("discovery is nil")
	}
	err := discovery.Validate()
	if err == nil {
		return nil, err
	}
	DiscoveryType := strings.ToLower(string(discovery.Type))
	switch DiscoveryType {
	case string(options.RegistryDiscoveryTypeNativeType):
		return native_config.NewNativeConfigDiscovery(discovery.Address), nil
	case string(options.RegistryDiscoveryTypeConsul):
		return consul.NewConsulDiscovery(discovery), nil
	case string(options.RegistryDiscoveryTypeEtcd):
		return etcd.NewEtcdDiscovery(discovery), nil
	case string(options.RegistryDiscoveryTypeEureka):
		return eureka.NewEurekaDiscovery(discovery), nil
	case string(options.RegistryDiscoveryTypeKubernetes):
		return kubernetes.NewKubernetesDiscovery(discovery)
	case string(options.RegistryDiscoveryTypeNacos):
		return nacos.NewNacosDiscovery(discovery), nil
	case string(options.RegistryDiscoveryTypeZookeeper):
		return zookeeper.NewZookeeperDiscovery(discovery), nil
	}
	return nil, errors.New("discovery type not supported")
}
